package copilot

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"hzer/internal/middleware"
	"hzer/internal/mysql"
	jwtpkg "hzer/pkg/jwt"
	"hzer/pkg/util"
	"net/http"
	"strconv"
)

func getLoginUser(ctx *gin.Context) {
	token, err := jwtpkg.GetJwtProto(ctx, &middleware.UserLoad{})
	if err != nil {
		ctx.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
		return
	}
	ucli := mysql.NewUserCodeInstance()
	udata, err := ucli.GetUserCodeByCode(token.CardCode)
	if err != nil {
		ctx.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
		return
	}
	UID := 14223432 + udata.ID
	ctx.JSON(http.StatusOK, gin.H{
		"login":               "fakeuser",
		"id":                  UID,
		"node_id":             "MDQ6VXNlcj" + util.RandomStr(10),
		"avatar_url":          fmt.Sprintf("https://avatars.githubusercontent.com/u/%d?v=4", UID),
		"gravatar_id":         "",
		"url":                 "https://api.github.com/users/fakeuser",
		"html_url":            "https://github.com/fakeuser",
		"followers_url":       "https://api.github.com/users/fakeuser/followers",
		"following_url":       "https://api.github.com/users/fakeuser/following{/other_user}",
		"gists_url":           "https://api.github.com/users/fakeuser/gists{/gist_id}",
		"starred_url":         "https://api.github.com/users/fakeuser/starred{/owner}{/repo}",
		"subscriptions_url":   "https://api.github.com/users/fakeuser/subscriptions",
		"organizations_url":   "https://api.github.com/users/fakeuser/orgs",
		"repos_url":           "https://api.github.com/users/fakeuser/repos",
		"events_url":          "https://api.github.com/users/fakeuser/events{/privacy}",
		"received_events_url": "https://api.github.com/users/fakeuser/received_events",
		"type":                "User",
		"site_admin":          false,
		"name":                "Fake User " + strconv.Itoa(int(UID)),
		"company":             "FakeCompany",
		"blog":                "",
		"location":            "FakeCity",
		"email":               fmt.Sprintf("fakeuser%d@fakeemail.com", UID),
		"hireable":            nil,
		"bio":                 nil,
		"twitter_username":    nil,
		"public_repos":        10,
		"public_gists":        10,
		"followers":           100,
		"following":           100,
		"created_at":          udata.CreatedAt.UTC().Format("2006-01-02T15:04:05Z"),
		"updated_at":          udata.UpdatedAt.UTC().Format("2006-01-02T15:04:05Z"),
	})

}
